package com.yoda.mall.admin.service;

import com.yoda.common.exception.BusinessException;
import com.yoda.common.exception.ErrorResult;
import com.yoda.mall.api.AttributeApi;
import com.yoda.mall.api.ProductAttributeApi;
import com.yoda.mall.model.entity.pms.ProductAttribute;
import com.yoda.mall.model.entity.pms.ProductAttributeCategory;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.ProductAttributeVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wangjia
 * @date 2022/12/17
 * @Description
 */
@Service
public class ProductAttributeService {

    @DubboReference
    private ProductAttributeApi productAttributeApi;

    @DubboReference
    private AttributeApi attributeApi;


    //添加商品属性分类
    public void addProduct(String name) {
        Boolean flag = productAttributeApi.addProduct(name);
        if (!flag) {
            throw new BusinessException(ErrorResult.error());
        }
    }

    //根据id删除
    public void delete(Long id) {
        productAttributeApi.delete(id);
    }


    //获取单个商品属性分类信息
    public ProductAttributeCategory findById(Long id) {
        return productAttributeApi.findById(id);
    }

    //修改
    public void update(Long id, String name) {
        productAttributeApi.update(id,name);
    }

    //分页获取所有商品属性分类
    public PageResult<ProductAttributeCategory> list(Integer pageNum, Integer pageSize) {
        return productAttributeApi.list(pageNum,pageSize);

    }

    //获取所有商品属性分类及其下属性
    public List<ProductAttributeVo> findProduct() {
        //查询所有商品属性分类
        List<ProductAttributeCategory> categories = productAttributeApi.findAll();
       //查询所有分类属性
        List<ProductAttribute> attributes = attributeApi.getAllattribute();
        //遍历商品属性分类
        ArrayList<ProductAttributeVo> vos = new ArrayList<>();
        for (ProductAttributeCategory category : categories) {
            ProductAttributeVo vo = new ProductAttributeVo();
            BeanUtils.copyProperties(category,vo);
            ArrayList<ProductAttribute> list = new ArrayList<>();
            for (ProductAttribute attribute : attributes) {
                if (Objects.equals(category.getId(),attribute.getProductAttributeCategoryId())) {
                    list.add(attribute);
                }
            }
            for (int i = 0; i < attributes.size(); i++) {
                if (category.getId().equals(attributes.get(i).getProductAttributeCategoryId())) {
                    list.add(attributes.get(i));
                    attributes.remove(i);
                    i--;
                }
            }
            vo.setProductAttributeList(list);
            vos.add(vo);
        }
        return vos;
    }
    //添加商品属性
    public void create(ProductAttribute attribute) {
        attributeApi.create(attribute);
    }
    //批量删除商品属性
    public void deleteIds(List<Integer> ids) {
        attributeApi.deleteIds(ids);
    }

    public ProductAttribute findOne(Long id) {
       return attributeApi.findOne(id);
    }

    //根据商品分类id获取商品属性及属性分类
    public PageResult findPage(Long cid, Integer type, Integer pageNum, Integer pageSize) {
        //调用api获取分页结果
        List<ProductAttribute> list = attributeApi.findPage(cid, type, pageNum, pageSize);
       return new PageResult(pageNum,pageSize,list.size(),list);
    }

    //根据商品分类id获取商品属性及属性分类
    public ProductAttribute findAttribute(Long productCategoryId) {
         return productAttributeApi.findAttribute(productCategoryId);
    }
}
